<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Manager
 *
 * @author Administrator
 */
class Manager extends CActiveRecord {

    public function rules() {
        return array(
            array('admin_name,admin_email', 'required'),
            array('admin_pass', 'required', "on" => "insert"),
            array('admin_name, admin_pass', 'length', 'max' => 50, 'min' => 5),
            array('admin_email', 'email'),
            array('admin_name', 'unique' ),
            array('admin_group_id', 'required','message' => "请选择一个权限组" )
        );
    }

    public function attributeLabels() {
        return array(
             'admin_id' => Yii::t('default', '编号'),
            'admin_name' => Yii::t('default', '帐户名'),
            'admin_pass' => Yii::t('default', '密码'),
            'admin_email' => Yii::t('default', '联系邮箱'),
            'admin_group_id' => Yii::t('default', '管理员组'),
            'admin_create_time' => Yii::t('default', '帐户创建时间'),
            'admin_create_ip' => Yii::t('default', '帐户创建IP'),
            'admin_last_login' => Yii::t('default', '帐户登录时间'),
            'admin_login_error' => Yii::t('default', '登录错误数'),
            'admin_create_user' => Yii::t('default', '帐户创建者'),
            'admin_status' => Yii::t('default', '帐户状态'),
            'admin_add_title' => Yii::t('default', '新增管理员帐户'),
            'admin_edit_title' => Yii::t('default', '编辑管理员帐户')
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{admin}}';
    }

    public function getSha1PassWord($passwd) {
        return sha1($passwd);
    }

    /**
     * 组织用户组下拉菜单
     * @return type 
     */
    public function getParentOption() {
        return ManagerGroup::model()->getParentOption();
    }

    /**
     *
     * @param integer $usergroupId
     * @return String
     */
    public function getUserGroupName($usergroupId) {
        return ManagerGroup::model()->findByPk($usergroupId)->admin_group_name;
    }

    public function getUserStatusName($name) {
        return User::model()->getUserStatusName($name);
    }
    public function getUserStatusArray(){
       return User::model()->getUserStatusArray($name);
    }
        /**
     * 在数据保存之前执行
     * @return boolean
     */
    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord){
                $this->admin_pass = $this->getSha1PassWord($this->admin_pass);
                $this->admin_last_login = time();
                $this->admin_create_time = time();
                $this->admin_create_ip = Yii::app()->request->getUserHost();
                $this->admin_create_user = Cookie::get('admin');
            }else{
                if( strlen($this->admin_pass) != 40){
                    $this->admin_pass = $this->getSha1PassWord($this->admin_pass);
                }
            }
            return true;
        }
        else
            return false;
    }
}

?>
